package tech.aistar.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tech.aistar.constants.MessageConstant;
import tech.aistar.pojo.User;
import tech.aistar.pojo.dto.UserRegisterDTO;
import tech.aistar.pojo.result.ResultReturn;
import tech.aistar.service.UserService;
import tech.aistar.utils.RegexUtils;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户名登录
     * @param user
     * @return
     */
    @PostMapping("/login/username")
    public ResultReturn loginByName(@RequestBody User user) {
        log.info("使用用户名登录:{}",user);
        if (user == null ||
                user.getUsername() == null || user.getPassword() == null ||
                user.getUsername().isEmpty() || user.getPassword().isEmpty()) {
            return ResultReturn.fail(MessageConstant.NAME_OR_PWD_NULL);
        }
        return userService.loginByName(user);
    }
    /**
     * 验证码登录
     * @param userRegisterDTO
     * @return
     */
    @PostMapping("/login/code")
    public ResultReturn loginByCode(@RequestBody UserRegisterDTO userRegisterDTO) {
        log.info("使用验证码登录：{}",userRegisterDTO);
        return userService.loginByCode(userRegisterDTO);
    }
    /**
     * 手机发送登录验证码
     * @param phone
     * @return
     */
    @PostMapping("/login/sendCode/phone")
    public ResultReturn sendCodeByPhone2(@RequestHeader("phone") String phone) {
        //校验格式
        if (RegexUtils.isPhoneInvalid(phone)) {
            return ResultReturn.fail(MessageConstant.PHONE_ERR);
        }
        return userService.sendCodeLoginPhone(phone);
    }
    /**
     * 邮箱发送登录验证码
     * @param email
     * @return
     */
    @PostMapping("/login/sendCode/email")
    public ResultReturn sendCodeByEmail2(@RequestHeader("email") String email) {
        //校验格式
        if (RegexUtils.isEmailInvalid(email)) {
            return ResultReturn.fail(MessageConstant.EMAIL_ERR);
        }
        return userService.sendCodeLoginEmail(email);
    }

    /**
     * 手机发送注册验证码
     * @param phone
     * @return
     */
    @PostMapping("/register/sendCode/phone")
    public ResultReturn sendCodeByPhone(@RequestHeader("Phone") String phone) {
        //校验格式
        if (RegexUtils.isPhoneInvalid(phone)) {
            return ResultReturn.fail(MessageConstant.PHONE_ERR);
        }
        return userService.sendCodeRegisterPhone(phone);
    }
    /**
     * 邮箱发送注册验证码
     * @param email
     * @return
     */
    @PostMapping("/register/sendCode/email")
    public ResultReturn sendCodeByEmail(@RequestHeader("Email") String email) {
        //校验格式
        if (RegexUtils.isEmailInvalid(email)) {
            return ResultReturn.fail(MessageConstant.EMAIL_ERR);
        }
        return userService.sendCodeRegisterEmail(email);
    }

    /**
     * 用户注册
     * @param userRegisterDTO
     * @return
     */
    @PostMapping("/register")
    public ResultReturn register(@RequestBody UserRegisterDTO userRegisterDTO) {
        log.info("用户注册：{}",userRegisterDTO);
        if (userRegisterDTO == null ||
                userRegisterDTO.getUsername() == null || userRegisterDTO.getPassword() == null ||
                userRegisterDTO.getUsername().isEmpty() || userRegisterDTO.getPassword().isEmpty() ||
                 userRegisterDTO.getCode() == null || userRegisterDTO.getCode().isEmpty()) {
            return ResultReturn.fail(MessageConstant.PARAM_ERR);
        }
        if (userRegisterDTO.getEmail().isEmpty() && userRegisterDTO.getPhone().isEmpty() ||
            !userRegisterDTO.getEmail().isEmpty() && !userRegisterDTO.getPhone().isEmpty()) {
            return ResultReturn.fail(MessageConstant.PARAM_ERR);
        }
        if ("".equals(userRegisterDTO.getPhone())){
            userRegisterDTO.setPhone(null);
        }
        if ("".equals(userRegisterDTO.getEmail())){
            userRegisterDTO.setEmail(null);
        }
        return userService.register(userRegisterDTO);
    }
}
